/*  Game/Army Editor for yALB - Yet Another List Builder (roster Editor for Wargames/tabletop games).
    Copyright (C) 2008-2012  Daniel Ricardo Castro Alvarado

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.*/

#include "itemwidget.h"
#include "ui_itemwidget.h"

#include "other/functions.h"
#include "other/modelcommon.h"
#include "other/categorizermodel.h"
#include "widgets/itemselector.h"

#include <QDebug>
#include <QMessageBox>

ItemWidget::ItemWidget(ItemCollection *_items, CategorizerModel *_itemModel, ItemSelector &_itemSelector,
                       QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ItemWidget)
{
    Q_ASSERT(_items && _itemModel);
    ui->setupUi(this);
    items = _items;
    itemModel = _itemModel;

    ui->itemList->setModel(_itemModel);
    ui->itemCategory->setModel(_itemModel);
    ui->itemCategory->setCurrentIndex(0);

    ui->scrollArea_2->setAlignment(Qt::AlignCenter);

    connect(ui->itemList->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
            this, SLOT(currentItemChanged(QModelIndex,QModelIndex)));
}

ItemWidget::~ItemWidget()
{
    delete ui;
}

void ItemWidget::clearItemForm() {
    ui->actionBox->clearActionForm();
    ui->itemName->setText("");
    ui->itemCost->setValue(0.0);
    ui->itemKey->setText("");
    ui->itemPointsPerMiniature->setChecked(false);
    ui->itemMaxNumber->setValue(0);
}

void ItemWidget::currentItemChanged(const QModelIndex& current, const QModelIndex& previous) {
    Q_UNUSED(previous);
    propertyForm.clearAllValues();
    propertyForm.savePropertyValues();

    if(current.isValid() && !itemModel->isCategory(current)) {
        QString key = current.data(ModelCommon::KeyRole).toString();
        const GameItem& item = items->get(key);
        ui->itemCategory->setCurrentIndex(current.parent().row());
        ui->itemName->setText(item.getName());
        ui->itemCost->setValue(item.getCost());
        ui->itemKey->setText(key);
        ui->itemPointsPerMiniature->setChecked(item.costPerMin);
        ui->itemMaxNumber->setValue(item.getMaximumNumber());
        ui->actionBox->setActions(item.actions);
        ui->itemDelete->setEnabled(true);
        ui->itemUpdate->setEnabled(true);
        ui->itemClone->setEnabled(true);

        // Set up PropertyForm

        QHash<QString, QString>::const_iterator i;
        for(i = item.properties.constBegin();
            i != item.properties.constEnd(); ++i)
            propertyForm.setPropertyValue(i.key(), i.value());

        ui->itemPropertiesBox->setEnabled(true);
    } else {
        clearItemForm();
        ui->itemCategory->setCurrentIndex(current.row());
        ui->itemDelete->setEnabled(false);
        ui->itemUpdate->setEnabled(false);
        ui->itemClone->setEnabled(false);

        ui->itemPropertiesBox->setEnabled(false);
    }

}

void ItemWidget::on_itemAdd_clicked()
{
    GameItem item(ui->itemCategory->itemData(ui->itemCategory->currentIndex(),ModelCommon::KeyRole).toString(), ui->itemName->text(), (float)ui->itemCost->value(),
                  ui->itemPointsPerMiniature->isChecked(), ui->itemMaxNumber->value(), ui->actionBox->getActions(),
                  QHash<QString, QString>());
    items->add(item, ui->itemKey->text());
}

void ItemWidget::on_itemUpdate_clicked()
{
    QString oldKey = ui->itemList->currentIndex().data(ModelCommon::KeyRole).toString();
    GameItem oldItem = items->get(oldKey);
    GameItem newItem(ui->itemCategory->itemData(ui->itemCategory->currentIndex(),ModelCommon::KeyRole).toString(), ui->itemName->text(), (float)ui->itemCost->value(),
                     ui->itemPointsPerMiniature->isChecked(), ui->itemMaxNumber->value(), ui->actionBox->getActions(),
                     oldItem.properties);
    items->update(oldKey, newItem, ui->itemKey->text());
}

void ItemWidget::on_itemDelete_clicked()
{
    if(QMessageBox::warning(0, tr("Are you sure?"), tr("Deleting an item will make invalid all values that use it. Are you sure you want to continue?"),
                               QMessageBox::Yes | QMessageBox::No, QMessageBox::No) == QMessageBox::Yes)
        items->remove(ui->itemList->currentIndex().data(ModelCommon::KeyRole).toString());
}

void ItemWidget::on_itemClone_clicked()
{
    QString key = ui->itemList->currentIndex().data(ModelCommon::KeyRole).toString();
    items->cloneKey(key);
}

void ItemWidget::on_itemSetPropertiesButton_clicked()
{
    QString key = ui->itemList->currentIndex().data(ModelCommon::KeyRole).toString();
    if(!key.isEmpty() && propertyForm.exec() == QDialog::Accepted) {
        GameItem item = items->get(key);
        item.properties = propertyForm.getProperties();
        items->update(key, item, key);
    }
}

void ItemWidget::changeEvent(QEvent *event) {
    if(event->type() == QEvent::LanguageChange)
        ui->retranslateUi(this);
    QWidget::changeEvent(event);
}
